package com.zhiren.settlement;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.script.ScriptException;

import com.zhiren.common.CustomMaths;
import com.zhiren.common.ListTools;
import com.zhiren.formular.Calculator;

import java.util.Map.Entry;

public class Settlement_old1 {
	public static final String SettlementType001 = "001"; 
	public static final String SettlementType002 = "002"; 
	
	public static final String ResultType_Weighted_Sum ="WeightedSum";	//先计算各项单价的加权平均然后加权平均单价和为总单价
	public static final String ResultType_Sum_Weighted ="SumWeighted";	//先计算各项单价的和为各批总单价然后计算加权平均单价
	
	
	static Map<String,String> propsmap;
	static Map<String,String> roundingmap;
	public String getPriceName(){
		return propsmap.get("PriceName");
	}
	
	public String getDeductionName(){
		return propsmap.get("DeductionName");
	}
	
	public String getMAP_KEY_BATCHCODE(){
		return propsmap.get("MAP_KEY_BATCHCODE");
	}
	
	public String getSUFFIX_TARGET(){
		return propsmap.get("SUFFIX_TARGET");
	}
	
	public String getMAP_KEY_AMOUNT_MONEY(){
		return propsmap.get("MAP_KEY_AMOUNT_MONEY");
	}
	
	public String getMAP_KEY_PRICE(){
		return propsmap.get("MAP_KEY_PRICE");
	}
	
	public String getMAP_KEY_AMOUNT(){
		return propsmap.get("MAP_KEY_AMOUNT");
	}
	
	public String getSUMMARYTYPE(){
		return propsmap.get("SUMMARYTYPE");
	}
	
	public String getMAP_KEY_PRICECODE(){
		return propsmap.get("MAP_KEY_PRICECODE");
	}
	
	public String getMAP_KEY_PRICETYPE(){
		return propsmap.get("MAP_KEY_PRICETYPE");
	}
	
	public String getPriceScale(){
		return roundingmap.get("PriceScale");
	}
	
	public Settlement_old1() throws UnsupportedEncodingException, IOException{
//		读取配置文件
		InputStream is = SettlementOp.class
				.getResourceAsStream("definition.properties");
		if (is != null) {
			
			Properties props = new Properties();
			props.load(new InputStreamReader(is,"UTF-8"));
			
			propsmap = new HashMap<String,String>();
		    Iterator<Entry<Object, Object>> itr = props.entrySet().iterator();
		    while (itr.hasNext()) {
		      Entry<Object, Object> e = (Entry<Object, Object>) itr.next();
//		      System.out.println(e);
		      propsmap.put(e.getKey().toString(), e.getValue().toString());
		    }
			is.close();
		}
//		读取小数位配置文件
		is = SettlementOp.class
				.getResourceAsStream("rounding-default.properties");
		if (is != null) {
			
			Properties props = new Properties();
			props.load(new InputStreamReader(is,"UTF-8"));
			
			roundingmap = new HashMap<String,String>();
		    Iterator<Entry<Object, Object>> itr = props.entrySet().iterator();
		    while (itr.hasNext()) {
		      Entry<Object, Object> e = (Entry<Object, Object>) itr.next();
//		      System.out.println(e);
		      roundingmap.put(e.getKey().toString(), e.getValue().toString());
		    }
			is.close();
		}
	}
	
//	private String getConvertField(String oValue){
//		return propsmap.get(oValue);
//	}
//	
//	private String convertDef(String strEval){
//		String value = "";
//		Iterator<Entry<String, String>> itr = propsmap.entrySet().iterator();
//		while (itr.hasNext()) {
//	      Entry<String, String> e = (Entry<String, String>) itr.next();
//	      value = strEval.replaceAll(e.getKey().toString(), e.getValue().toString());
//	    }
//		return value;
//	}
	/**
	 * 
	 * @param conditionList				价格条款list
	 * @param sourcesList				源数据list
	 * @param pricebatch				加权质量批次
	 * @param rownum					分组行号
	 * @param priceMode					结算价格类型（价格/增扣）
	 * @return
	 * @throws NumberFormatException
	 * @throws ScriptException
	 * @throws Exception
	 */
	private List<Map<String,String>> doWeightingPrice(List<Map<String,String>> conditionList, List<Map<String,String>> sourcesList, 
			Map<String,String> pricebatch, String rownum, String priceMode) throws Exception{
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		for(int i=0; i< conditionList.size(); i++){	//循环结算价格条款
			Map<String,String> pricerow = 
					conditionList.get(i);
			String groupnum = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_GROUPNUM);
			String code = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE);
			String eval = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_EVAL);
			String formula = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_FORMULA);
			String formulaparam = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_FORMULAPARA);
			List<String> allkeys = SettlementOp.getConditionKeys(eval);
			if(SettlementOp.inArray(groupnum, rownum, false)){	//[分组行号相等或分组行号为空]
				try{
					if(SettlementOp.judgeCondition(allkeys, eval, pricebatch)){	//符合计价条件
						List<Map<String,String>> groupBatch = ListTools.cloneListMap(sourcesList);
						//替换公式中指标的符号为正确的值
						formula = SettlementOp.replaceFormula(formula, allkeys, pricebatch);
						if(formulaparam!=null&& !"".equals(formulaparam))
						formula = SettlementOp.replaceFormulaParam(formula, formulaparam);
						//计算更新每批次单价
						double price = Calculator.getCalculatorValue(formula);
						//将得出的单价写入该组各批次数据中
						for(int j=0; j< groupBatch.size(); j++){
							Map<String,String> batch = groupBatch.get(j);
							price = Double.parseDouble(batch.get(code)==null?"0":batch.get(code)) + price;
							//写入价格
							batch.put(code, String.valueOf(price));
							//写入计价分类
							batch.put(SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE, code);
							//写入计算处理的分类（价格/增扣）
							batch.put(SettlementOp.PRICE_MODE_KEY, priceMode);
							//将计算加权后的指标写入批次
							Iterator<Entry<String, String>> it = pricebatch.entrySet().iterator();
							while(it.hasNext()){
								Entry<String, String> e = (Entry<String, String>)it.next();
								batch.put(e.getKey() + getSUFFIX_TARGET(), String.valueOf(e.getValue()) );
							}
						}
						result.addAll(groupBatch);
					}
				}catch(ScriptException s){
					throw new Exception("计算公式："+eval+" 出错");
				}
			}
		}
		return result;
	}
	
	/**
	 * 
	 * @param conditionList					条件list
	 * @param sourcesList					源数据list
	 * @param rownum						分组行号
	 * @param priceMode						结算价格类型（价格/增扣）
	 * @return
	 * @throws NumberFormatException
	 * @throws ScriptException
	 * @throws Exception
	 */
	private List<Map<String,String>> doBatchPrice(List<Map<String,String>> conditionList, List<Map<String,String>> sourcesList, 
			String rownum, String priceMode) throws Exception{
		List<Map<String,String>> sourcestmp = ListTools.cloneListMap(sourcesList);
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		for(int i=0; i < sourcestmp.size(); i ++){	//循环组内单批次
			
//			Map<String,String> pricebatch = sourcestmp.get(i);
			for(int pr=0; pr< conditionList.size(); pr++){	//循环结算价格条款
				
				Map<String,String> pricerow = conditionList.get(pr);
				String groupnum = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_GROUPNUM);
				String code = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE);
				String eval = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_EVAL);
				String formula = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_FORMULA);
				String formulaparam = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_FORMULAPARA);
				List<String> allkeys = SettlementOp.getConditionKeys(eval);
				if(SettlementOp.inArray(groupnum, rownum, false)){	//[分组行号相等或分组行号为空]
					Map<String,String> pricebatch = new HashMap<String,String>();
					pricebatch.putAll(sourcestmp.get(i));
					try{
					if(SettlementOp.judgeCondition(allkeys, eval, pricebatch)){	//符合计价条件
						//替换公式中指标的符号为正确的值
						formula = SettlementOp.replaceFormula(formula, allkeys, pricebatch);
						if(formulaparam!=null&& !"".equals(formulaparam))
						formula = SettlementOp.replaceFormulaParam(formula, formulaparam);
						//计算更新每批次单价
						double price = Calculator.getCalculatorValue(formula);
						//将得出的单价写入批次数据中
						price = Double.parseDouble(pricebatch.get(code)==null?"0":pricebatch.get(code)) + price;
						pricebatch.put(code, String.valueOf(price));
						//写入计价分类
						pricebatch.put(SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE, code);
						//写入计算处理的分类（价格/增扣）
						pricebatch.put(SettlementOp.PRICE_MODE_KEY, priceMode);
						//将计算加权后的指标写入批次								
						List<String> l = allkeys;
						for(int k = 0; k< l.size(); k++){
							String key = l.get(k);
							pricebatch.put(key+getSUFFIX_TARGET(), pricebatch.get(key));
						}
						result.add(pricebatch);
					}
					}catch(ScriptException s){
						throw new Exception("计算公式："+eval+" 出错");
					}
				}
			}
		}
		return result;
	}
	
	private void getResultSummary(String summaryKey, List<Map<String,String>> result, List<Map<String,String>> sources, String resultType, List<String> allKeys) throws Exception{
		String conditioneval = SettlementOp.TMP_GROUP_MAP_KEY_PRICETYPE+SettlementOp.SIGN_EQUAL+"'"+summaryKey+"'";
		List<Map<String,String>> sourceList = null;
		try{
			sourceList = ListTools.cloneListMap(SettlementOp.extract(conditioneval, sources));
		}catch(ScriptException s){
			throw new Exception("解析公式："+conditioneval+" 出错");
		}
		List<String> keys = new ArrayList<String>();
		//将各种价格写入map，并根据费用项目形成合计价格
		for(int i=0; i< sourceList.size(); i++){
			Map<String,String> m = sourceList.get(i);
			String key = m.get(SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE);
			keys.add(key);
			String value = m.get(key)==null?"0":m.get(key);
			String oldVal = "0";
			String sumPrice = "0";
			int batchIndex = Integer.parseInt(m.get(getMAP_KEY_BATCHCODE()))-1;
			if(result.get(batchIndex) == null){
				result.set(batchIndex, m);
			}else{
				m = result.get(batchIndex);
				Map<String,String> t = sourceList.get(i);
				for(int j=0; j< allKeys.size(); j++){
					String _key = allKeys.get(j) + getSUFFIX_TARGET();
					String _val = t.get(_key);
					if(_val!=null && !"".equals(_val)){
						m.put(_key, _val);
					}
				}
				
				oldVal = m.get(key)==null?"0":m.get(key);
				sumPrice = m.get(summaryKey)==null?"0":m.get(summaryKey);
			}
			
			m.put(key, String.valueOf(CustomMaths.round(Double.parseDouble(value) + Double.parseDouble(oldVal),2)));
			m.put(summaryKey, String.valueOf(CustomMaths.round(Double.parseDouble(value) + Double.parseDouble(sumPrice),2)));
			
			result.set(batchIndex, m);
		}
		keys = new ArrayList<String>(new HashSet<String>(keys));
		
		if(ResultType_Weighted_Sum.equals(resultType)){
			double sumprice = 0.0;
			for(int i=0; i< keys.size(); i++){
				double price = SettlementOp.getWeightedValue(getMAP_KEY_AMOUNT(), keys.get(i), result, Integer.parseInt(getPriceScale()));
				sumprice += price;
				for(int j=0; j< result.size(); j++){
					Map<String,String> m = result.get(j);
					m.put("price_"+keys.get(i), String.valueOf(price));
				}
			}
			sumprice = CustomMaths.round(sumprice, 2);
			for(int i=0; i< result.size(); i++){
				Map<String,String> m = result.get(i);
				double amount = Double.parseDouble(m.get(getMAP_KEY_AMOUNT()));
				double Amountofcoal = CustomMaths.round(amount * sumprice, 2);
				m.put(getMAP_KEY_PRICE(), String.valueOf(sumprice));
				m.put(getMAP_KEY_AMOUNT_MONEY(), String.valueOf(Amountofcoal));
			}
		}else if(ResultType_Sum_Weighted.equals(resultType)){
			double price = SettlementOp.getWeightedValue(getMAP_KEY_AMOUNT(), summaryKey, result, Integer.parseInt(getPriceScale()));
			for(int i=0; i< result.size(); i++){
				Map<String,String> m = result.get(i);
				double amount = Double.parseDouble(m.get(getMAP_KEY_AMOUNT()));
				double Amountofcoal = CustomMaths.round(amount * price, 2);
				m.put(getMAP_KEY_PRICE(), String.valueOf(price));
				m.put(getMAP_KEY_AMOUNT_MONEY(), String.valueOf(Amountofcoal));
			}
		}
		
	}
	
	
	private List<Map<String,String>> Calculation(List<Map<String,String>> sources,SettlementTemplate balanceTemplate, String SettlementType) throws Exception{
//		参数验证
		if(sources == null || sources.isEmpty()){
			throw new Exception("结算数据为空");
		}
		if(balanceTemplate == null){
			throw new Exception("结算方案模板为空");
		}
		if(SettlementType == null || "".equals(SettlementType)){
			throw new Exception("结算类型为空");
		}
//		定义变量
		
		List<Map<String,String>> bisic = new ArrayList<Map<String,String>>();	//计算结果
		
		List<Map<String,String>> sourcestmp = sources;	//得到结算数据
		
		List<String> allkeys = new ArrayList<String>();	//计价或增扣款key
		List<String> funcKeys = new ArrayList<String>();	//公式的key
		List<String> summaryKeys = new ArrayList<String>();//汇总分类
		List<String> independKeys = new ArrayList<String>();//汇总分类
		List<String> independCodes = new ArrayList<String>();//计价分类
		
		//设置批次自动编码
		for(int i=0; i< sourcestmp.size(); i++){
			Map<String,String> m = sourcestmp.get(i);
			m.put(getMAP_KEY_BATCHCODE(), String.valueOf(i+1));
		}

		//获取公式key
		for(int i=0 ; i<Calculator.funcArray.length; i++){
			funcKeys.add(Calculator.funcArray[i]);
		}
		
		//获取条件、计价、增扣款keys
		for(int i = 0; i< balanceTemplate.getGroupList().size(); i++){
			Map<String,String> grouprow = balanceTemplate.getGroupList().get(i);
			String priceType = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_PRICETYPE);	//数量结算依据指标代码
			allkeys.addAll(SettlementOp.getConditionKeys(grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_EVAL)));
			String rownum = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_ROWNUM);
			//写入费用类型（煤款、运费、杂费等）
			if(SettlementOp.inArray(getSUMMARYTYPE(), priceType, false)){
				summaryKeys.add(priceType);
			}else{
				independKeys.add(priceType);
				for(int j = 0; j< balanceTemplate.getPriceTermsList().size(); j++){
					Map<String,String> pricerow = balanceTemplate.getPriceTermsList().get(j);
					String groupnum = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_GROUPNUM);
					String code = (String)pricerow.get(SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE);
					if(SettlementOp.inArray(groupnum, rownum, false))
						independCodes.add(code);
				}
				
			}
		}
		
		for(int i = 0; i< balanceTemplate.getPriceTermsList().size(); i++){
			Map<String,String> m = balanceTemplate.getPriceTermsList().get(i);
			allkeys.addAll(SettlementOp.getConditionKeys(m.get(SettlementOp.TMP_TERMS_MAP_KEY_EVAL)));
		}
		for(int i = 0; i< balanceTemplate.getDeductionList().size(); i++){
			allkeys.addAll(SettlementOp.getConditionKeys(balanceTemplate.getPriceTermsList().get(i).get(SettlementOp.TMP_TERMS_MAP_KEY_EVAL)));
		}
		allkeys = new ArrayList<String>(new HashSet<String>(allkeys));
		allkeys.removeAll(funcKeys);
		summaryKeys = new ArrayList<String>(new HashSet<String>(summaryKeys));
		independKeys = new ArrayList<String>(new HashSet<String>(independKeys));
		independCodes = new ArrayList<String>(new HashSet<String>(independCodes));
		
		//循环分组表达式
		for(int gr=0; gr< balanceTemplate.getGroupList().size(); gr++){
			Map<String,String> grouprow = balanceTemplate.getGroupList().get(gr);
			String rownum = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_ROWNUM);
			String gEval = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_EVAL);
			String gMode = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_MODE);
			String priceType = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_PRICETYPE);	//费用类型
			String QuantityKey = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_Quantity);	//数量结算依据指标代码
			//QuantityKey = propsmap.get(QuantityKey)==null?QuantityKey:propsmap.get(QuantityKey);
			String QualityKey = (String)grouprow.get(SettlementOp.TMP_GROUP_MAP_KEY_Quality);	//质量结算依据指标代码
			if(QuantityKey==null || "".equals(QuantityKey)){
				throw new Exception("结算方案模板分组不正确，数量结算依据指标代码为空。");
			}
			if(QualityKey==null || "".equals(QualityKey)){
				throw new Exception("结算方案模板分组不正确，质量结算依据指标代码为空。");
			}
			if(priceType==null || "".equals(priceType)){
				throw new Exception("结算方案模板分组不正确，费用类型为空。");
			}
			if(gMode==null || "".equals(gMode)){
				throw new Exception("结算方案模板分组不正确，结算模式为空。");
			}
			if(rownum==null || "".equals(rownum)){
				throw new Exception("结算方案模板分组不正确，分组行号为空。");
			}
			if(gEval==null || "".equals(gEval)){
				throw new Exception("结算方案模板分组不正确，分组表达式为空。");
			}
			
			QualityKey = propsmap.get(QualityKey)==null?QualityKey:propsmap.get(QualityKey);
			String suffix = QualityKey;
			if(suffix==null){
				suffix = "_cf";
//				throw new Exception("结算质量依据参数错误");
			}
			
			//重构结算质量数据（设置结算指标）
			for(int i = 0; i< sourcestmp.size(); i++){
				Map<String,String> m = sourcestmp.get(i);
				for(int j = 0; j< allkeys.size(); j++){
					String key = allkeys.get(j);
					String OrginalUnit = m.get(key+suffix+"_dw");
					if(OrginalUnit==null || OrginalUnit.equals("")){
						m.put(key,m.get(key+suffix));
					}else{
						String converteval = propsmap.get(OrginalUnit.toLowerCase());
						if(converteval==null || "".equals(converteval)){
							throw new Exception("配置错误，未设置单位转换公式。");
						}
						m.put(key, SettlementOp.UnitConvertIgnoreCase(key, converteval, m.get(key+suffix)));
					}
				}
				//处理结算量
				List<String> keys = SettlementOp.getConditionKeys(QuantityKey);	//得到结算量公式的keys
				String amount = String.valueOf(Calculator.getCalculatorValue(SettlementOp.replaceFormula(QuantityKey, keys, m))); //计算结算量公式
				m.put(getMAP_KEY_AMOUNT(), amount);	//将结算量赋值在记录集Map里
			}
			
			
			//得到分组对应批
			List<Map<String,String>> groupBatch;
			if(gEval==null || "".equals(gEval)){	//如果表达式为空默认所有批一个分组
				groupBatch = ListTools.cloneListMap(sourcestmp);
			}else{
				groupBatch = ListTools.cloneListMap(SettlementOp.extract(gEval, sourcestmp));
			}
			if(groupBatch==null || groupBatch.size()==0){
				continue;
			}
			//将组内的费用分类计入基础数据
			for(int i=0;i< groupBatch.size(); i++){
				Map<String,String> sm = groupBatch.get(i);
				sm.put(SettlementOp.TMP_GROUP_MAP_KEY_PRICETYPE, priceType);
			}
			
			//计算价格得到结果
			if(SettlementOp.PRICE_MODE_WEIGHTING.equals(gMode)){	//计价方式加权
				//加权组内数据构成结算批
				Map<String,String> pricebatch = SettlementOp.getWeightedMeans(QuantityKey, allkeys, groupBatch, roundingmap);
				//处理价格
				if(balanceTemplate.getPriceTermsList().size()>0)
					bisic.addAll(doWeightingPrice(balanceTemplate.getPriceTermsList(), groupBatch, pricebatch, rownum, getPriceName()));
				//处理增扣
				if(balanceTemplate.getDeductionList().size()>0)
					bisic.addAll(doWeightingPrice(balanceTemplate.getDeductionList(), groupBatch, pricebatch, rownum, getDeductionName()));
			}else{												//计价方式单批次
				//处理价格
				if(balanceTemplate.getPriceTermsList().size()>0)
					bisic.addAll(doBatchPrice(balanceTemplate.getPriceTermsList(), groupBatch, rownum, getPriceName()));
				//处理增扣
				if(balanceTemplate.getDeductionList().size()>0)
					bisic.addAll(doBatchPrice(balanceTemplate.getDeductionList(), groupBatch, rownum, getDeductionName()));
			}
		}
		//结果返回
		
		String independEval = "";
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		//构建一个与sources同样长度的定长List
		List<Map<String,String>> template = new ArrayList<Map<String,String>>();
		for(int i=0; i< sources.size(); i++){
			template.add(null);
		}
		
		
		//算需要合计价格的费用项目
		for(int i=0; i< summaryKeys.size(); i++){
			List<Map<String,String>> rtmp = ListTools.cloneListMap(template);
			if(SettlementType001.equals(SettlementType)){
				getResultSummary(summaryKeys.get(i), rtmp, bisic, ResultType_Weighted_Sum, allkeys);
			}else if (SettlementType002.equals(SettlementType)){
				getResultSummary(summaryKeys.get(i), rtmp, bisic, ResultType_Sum_Weighted, allkeys);
			}
			
			result.addAll(rtmp);
			independEval +=  SettlementOp.TMP_GROUP_MAP_KEY_PRICETYPE+SettlementOp.SIGN_UNEQUAL+"'"+summaryKeys.get(i)+"'"+SettlementOp.SIGN_AND;
		}
		independEval = independEval.substring(0, independEval.length()-SettlementOp.SIGN_AND.length());
		
		//得到不需要合计价格的费用项目的记录
		List<Map<String,String>> sourceList = ListTools.cloneListMap(SettlementOp.extract(independEval, bisic));
		
		for(int i=0; i< independCodes.size(); i++){
			String conditioneval = SettlementOp.TMP_TERMS_MAP_KEY_PRICECODE+SettlementOp.SIGN_EQUAL+"'"+independCodes.get(i)+"'";
			List<Map<String,String>> rtmp = ListTools.cloneListMap(SettlementOp.extract(conditioneval, sourceList));
			String price = String.valueOf(SettlementOp.getWeightedValue(getMAP_KEY_AMOUNT(), independCodes.get(i), rtmp, Integer.parseInt(getPriceScale())));
			for(int j=0; j< rtmp.size(); j++){
				Map<String,String> m = rtmp.get(j);
				m.put(getMAP_KEY_PRICE(), price);
			}
			result.addAll(rtmp);
		}
		
		return result;
	}
	/**
	 * 
	 * @param sources			结算批数据
	 * @param balanceTemplate	结算方案
	 * @return
	 * @throws Exception
	 */
	public List<Map<String,String>> toBalance(List<Map<String,String>> sources,SettlementTemplate balanceTemplate, String settlementType) throws Exception{
		return Calculation(sources, balanceTemplate, settlementType);
	}
}
